package com.hsoft.zhxy.app.xtgl_02_01.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hsoft.zhxy.app.common.model.entity.JcqxBm;
import com.hsoft.zhxy.app.common.model.entity.JcqxYh;
import com.hsoft.zhxy.app.common.model.entity.JcqxYhbm;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcqxBmMapper;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcqxYhMapper;
import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcqxYhbmMapper;
import com.hsoft.zhxy.app.xtgl_02_01.model.query.YhglQuery;
import com.hsoft.zhxy.app.xtgl_02_01.model.vo.YhglVO;
import com.hsoft.zhxy.app.xtgl_02_01.service.JcqxYhService;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hsoft.zhxy.common.base.BaseService;
import com.hsoft.zhxy.common.result.ResultBody;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * All rights Reserved, Designed By 哈尔滨工业大学软件工程股份有限公司
 *
 * @Title: 用户 Service层接口实现类
 * @Package
 * @Description:
 * @author: system
 * @date: 2020-10-26
 */
@Log4j2
@Service
public class JcqxYhServiceImpl extends BaseService implements JcqxYhService {

    @Autowired
    private JcqxYhMapper jcqxYhMapper;

    @Autowired
    private JcqxBmMapper jcqxBmMapper;

    @Autowired
    private JcqxYhbmMapper yhbmMapper;


    /**
     * 查询单条数据
     *
     * @param jcqxYh
     * @return
     */
    public JSONObject findOne(JcqxYh jcqxYh) {
        JcqxYh rs = jcqxYhMapper.selectOne(new QueryWrapper<JcqxYh>().setEntity(jcqxYh));
        return ResultBody.setData(rs);
    }


    /**
     * 查询列表数据
     *
     * @param jcqxYh
     * @return
     */
    public JSONObject findList(JcqxYh jcqxYh) {
        List<JcqxYh> list = jcqxYhMapper.selectList(new QueryWrapper<JcqxYh>().setEntity(jcqxYh));
        return ResultBody.setData(list);
    }


    /**
     * 查询分页数据
     *
     * @param yhglQuery
     * @return
     */
    public JSONObject findListByPage(YhglQuery yhglQuery) {
        Page<YhglQuery> pageObj = new Page<>(yhglQuery.getPage(), yhglQuery.getPageSize());
        QueryWrapper<YhglQuery> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(yhglQuery.getSfzj() != null, "yh.sfzj", yhglQuery.getSfzj())
                .eq(yhglQuery.getYhzt() != null, "yh.yhzt", yhglQuery.getYhzt())
                .and(StringUtils.isNotBlank(yhglQuery.getIdxm()), wrapper -> wrapper.like("yh.yhid", yhglQuery.getIdxm()).or().like("yh.xm", yhglQuery.getIdxm()))
                .orderByAsc("yh.yhid");
        IPage<YhglVO> result = jcqxYhMapper.getYhPageList(pageObj, queryWrapper);
        return ResultBody.setData(result.getTotal(), result.getRecords());
    }


    /**
     * 新增
     * @param jcqxYh
     * @return
     */
    public JSONObject add(JcqxYh jcqxYh) {
        jcqxYh.setCjsj(LocalDateTime.now());
        jcqxYh.setCjr(getYhid());
        jcqxYh.setBgsj(LocalDateTime.now());
        jcqxYh.setBgr(getYhid());
        return ResultBody.setData(jcqxYhMapper.insert(jcqxYh));
    }


    /**
     * 修改
     * @param jcqxYh
     * @return
     */
    public JSONObject update(JcqxYh jcqxYh) {
        jcqxYh.setBgsj(LocalDateTime.now());
        jcqxYh.setBgr(getYhid());
        return ResultBody.setData(jcqxYhMapper.updateById(jcqxYh));
    }


    /**
     * 删除
     * @param jcqxYh
     * @return
     */
    public JSONObject delete(JcqxYh jcqxYh) {
        return ResultBody.setData(jcqxYhMapper.delete(new QueryWrapper<JcqxYh>().setEntity(jcqxYh)));
    }

    @Override
    public JSONObject deleteBatchYh(String str) {
        List<String> yhidList = new ArrayList<>();
        JSONObject json = JSON.parseObject(str);
        JSONArray ids = json.getJSONArray("ids");
        for (Object yhid : ids) {
            yhidList.add((String) yhid);
        }
        return ResultBody.setData(jcqxYhMapper.deleteBatchIds(yhidList));
    }

    @Override
    public JSONObject findBmList() {
        return ResultBody.setData(jcqxBmMapper.selectList(new QueryWrapper<JcqxBm>().orderByAsc("pxh")));
    }

    @Override
    public JSONObject addYhbm(JcqxYhbm yhbm) {
        yhbm.setSfzz(1);
        yhbm.setCjsj(LocalDateTime.now());
        yhbm.setCjr(getYhid());
        yhbm.setBgsj(LocalDateTime.now());
        yhbm.setBgr(getYhid());
        return ResultBody.setData(yhbmMapper.insert(yhbm));
    }
}
